package com.transcoachapp.adapter;


import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.transcoachapp.R;
import com.transcoachapp.data.Station;
import com.transcoachapp.data.Station;
import com.transcoachapp.database.DataBaseHelper;


public class AutoCompleteAdapter extends ArrayAdapter<Station> implements Filterable {
	private Activity context;
	private ArrayList<Station> items;
	private ArrayList<Station> itemsAll;
    private ArrayList<Station> suggestions;
    private int viewResourceId;
    
	public AutoCompleteAdapter(Context context,int viewResourceId ,int textViewResourceId,
			ArrayList<Station> objects) { 
		super(context, viewResourceId,textViewResourceId,objects);
		this.items = objects;
		this.itemsAll = (ArrayList<Station>) items.clone();
        this.suggestions = new ArrayList<Station>();
        this.viewResourceId = viewResourceId;
		this.context = (Activity) context;
	//	dbh = new DataBaseHelper(context);
	//	db=dbh.getReadableDatabase();
	//	dbAccess();
	}
	@Override
	 public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(viewResourceId, null);
        }
        Station customer = items.get(position);
        if (customer != null) {
            TextView stationName = (TextView) v.findViewById(R.id.autoCompleteStationName);
            TextView stationLocation = (TextView) v.findViewById(R.id.autoCompleteStationLocation);
            if (stationName != null) {
//              Log.i(MY_DEBUG_TAG, "getView Customer Name:"+customer.getName());
            	stationName.setText(customer.getStationName());
            }
            if (stationName != null) {
//              Log.i(MY_DEBUG_TAG, "getView Customer Name:"+customer.getName());
            	stationLocation.setText(customer.getLocation());
            }
        }
        return v;
    }

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (this.items != null)
		      return this.items.size();
		return 0;
	}
	@Override
	public Station getItem(int arg0) {
		// TODO Auto-generated method stub
		return this.items.get(arg0);
	}
	@Override
    public Filter getFilter() {
        return nameFilter;
    }

    Filter nameFilter = new Filter() {
        public String convertResultToString(Object resultValue) {
            String str = ((Station)(resultValue)).getStationName();
            return str;
        }
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if(constraint != null) {
                suggestions.clear();
                for (Station station : itemsAll) {
                    if(station.getStationName().toLowerCase().startsWith(constraint.toString().toLowerCase())){
                        suggestions.add(station);
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
                return filterResults;
            } else {
                return new FilterResults();
            }
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
        	
        	ArrayList<Station> filteredList = (ArrayList<Station>) results.values;
            if(results != null) {
            	clear();
            	if (results.count > 0) {
	                for (Station c : filteredList) {
	                    add(c);
	                }
	                notifyDataSetChanged();
                
                }else {	
                	notifyDataSetInvalidated();
                }
            	}
        }
    };
}
